package com.liuhao.ssm.controller;

import java.io.File;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.liuhao.ssm.domain.SysUser;
import com.liuhao.ssm.shiro.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.liuhao.ssm.util.DateUtil;

/**
 * Created by liuHao on 2017/5/24.
 */
public class BaseController {
    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected HttpSession session;
    private String rootPath;                                    //项目站点路径
    private String randNum;                                        //请求随机数
    private Date serverDate;                                    //服务器当前时间

    public String getRootPath() {
        this.rootPath = this.session.getServletContext().getRealPath(File.separator);
        return this.rootPath;
    }

    public String getRandNum() {
        this.randNum = "RandNum=" + Double.toString(Math.random());
        return randNum;
    }

    public Date getServerDate() {
        this.serverDate = DateUtil.getSysDate();
        return serverDate;
    }

    public SysUser getUser() {
        return ShiroUtils.getUserEntity();
    }

    public Integer getUserId() {
        Integer id = getUser().getUserId();
        return id;
    }

    @SuppressWarnings("rawtypes")
    public HashMap<String, Object> getParams() {
        HashMap<String, Object> rs = new HashMap<String, Object>();
        Enumeration<String> keys = request.getParameterNames();
        while (keys.hasMoreElements()) {
            String k = keys.nextElement();
            rs.put(k, request.getParameter(k));
        }
        return rs;
    }
}
